package com.sushe.service;


import com.sushe.dao.BedDao;
import com.sushe.dao.RoomDao;
import com.sushe.service.jiekou.RoomService;

import java.sql.SQLException;
import java.util.Scanner;

public class RoomServiceImpl implements RoomService {

    private RoomDao roomDao = new RoomDao();
    private BedDao bedDao = new BedDao();

    @Override
    public  void addRoom() throws SQLException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入添加的房间号: ");
        String roomNumber = scanner.nextLine();
        System.out.print("请输入添加的房间所属的楼栋编号: ");
        Integer  buildingId = scanner.nextInt();
        //判断该楼栋编号是否存在，不存在则报错并返回结束方法
        boolean flag=roomDao.selectByBuildingId(buildingId);
        if(!flag){
            System.out.println("该楼栋不存在");
            return;
        }
        //开始添加房间和房间中床位
        try{
            //添加对应房间
            roomDao.addRoom(roomNumber,buildingId);
            //为对应房间添加4个床位
            int roomId=roomDao.getRoomId(roomNumber);//获取房间Id
            for(int i=1;i<=4;i++){
                bedDao.addBed(i,roomId);
            }
            System.out.println("添加成功");
        }catch(Exception e){
            System.out.println("添加失败:"+e.getMessage());
        }
    }
}
